package com.jtpay.sdk.activity;

import android.content.Intent;
import android.os.Handler;

import com.jtpay.sdk.R;
import com.jtpay.sdk.base.BaseActivity;
import com.zhy.autolayout.AutoLinearLayout;

import java.util.ArrayList;
import java.util.List;

import apiUtils.apis.GetPlatFormAPI;
import apiUtils.models.PayInfo;
import apiUtils.models.PayPlatFormModel;
import appUtils.ToastUtil;
import butterknife.Bind;
import constant.ApiConstants;

public class WelcomeActivity extends BaseActivity {
    @Bind(R.id.welcome_layout)
    AutoLinearLayout mLayout;
    private List<PayPlatFormModel> mPayPlatFormModels = new ArrayList<>();
    private int mGetPlatFormCountLimit = 2;
    private int mGetPlatFormCurrent = 0;
    private GetPlatFormAPI mGetPlatFormAPI;

    @Override
    protected void initView() {
    }

    @Override
    protected void initData() {
        ToastUtil.init(this);
        ApiConstants.resetUrl();
        getPlatForm();
    }

    private void getPlatForm() {
        if (mGetPlatFormAPI == null)
            mGetPlatFormAPI = new GetPlatFormAPI();
        mGetPlatFormAPI.getPlatFormList("ALL", new GetPlatFormAPI.Listener() {
            @Override
            public void onSuccess(List<PayPlatFormModel> platFormModelList) {
                if (platFormModelList.size() > 0) {
                    mPayPlatFormModels.addAll(platFormModelList);
                    PayInfo.getInstance().setPayPlatFormModels(platFormModelList);
                    new Handler().postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            startActivity();
                        }
                    }, 2000);
                }
            }

            @Override
            public void onFailure(int code, String message) {
                if (++mGetPlatFormCurrent >= mGetPlatFormCountLimit) {
                    mLayout.setBackgroundResource(R.drawable.welcome_error);
                    return;
                }
                getPlatForm();
            }
        });
    }

    private void startActivity() {
        Intent intent = new Intent(WelcomeActivity.this, LoginActivity.class);
        startActivity(intent);
        finish();
    }


    @Override
    protected int setView() {
        return R.layout.activity_welcome;
    }
}
